﻿using AnlianCommon.Common.Domains.ProductionManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.EwesGiveBirthes
{
    public class EwesGiveBirthDeletedHandler : IRequestHandler<EwesGiveBirthDeletedCommand, Result>
    {
        private readonly IBaseRepository<EwesGiveBirth> _ewesGiveBirthRepository;

        public EwesGiveBirthDeletedHandler(IBaseRepository<EwesGiveBirth> ewesGiveBirthRepository)
        {
            _ewesGiveBirthRepository = ewesGiveBirthRepository;
        }
        /// <summary>
        /// 种母分娩删除业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<Result> Handle(EwesGiveBirthDeletedCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Deleted };
            try
            {
                var ewesGiveBirth = await (await _ewesGiveBirthRepository.GetAll()).Where(c => c.EwesGiveBirthId == request.EwesGiveBirthId).FirstOrDefaultAsync();
                if (ewesGiveBirth != null)
                {
                    ewesGiveBirth.IsDeleted = true;
                    ewesGiveBirth.DeletedOwnerId = "张三";
                    ewesGiveBirth.DeletedTime = DateTime.Now;
                    var res = await _ewesGiveBirthRepository.Update(ewesGiveBirth);
                    if (res > 0)
                    {
                        result.Code = (int)CommonErrorCode.Success;
                        result.Msg = "删除成功";
                    }
                    else
                    {
                        result.Code = (int)CommonErrorCode.Deleted;
                        result.Msg = "删除失败";
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
